//
// Copyright (c) 2024 BPer
//

#ifndef SRC_BPERRTC_BPERRTCSTREAM_H_
#define SRC_BPERRTC_BPERRTCSTREAM_H_

#include <bperutil/sys/BPerTime.h>
#include <bperrtp/BPerPublishNackBuffer.h>
#include <bperrtp/BPerReceiveNackBuffer.h>

#include <bperrtp/BPerRtpPacket.h>
#include <bperrtp/BPerRtpRecvNack.h>

#include <bperrtp/BPerRtcpTWCC.h>
#include <bperrtc/BPerRtcContextH.h>
#include <bperutil/buffer2/BPerSortBuffer.h>

typedef struct {

	BPerBool enableAudioBuffer;
	BPerBool isAudio;

	uint16_t last_seq;

	int32_t uid;
	uint32_t ssrc;

	int64_t last_sender_report_rtp_time;
	uint64_t last_sender_report_sys_time;

	BPerReceiveNackBuffer *rtp_queue;
	BPerRtpRecvNack *nack_receiver;

	BPerFrame audioFrame;
	BPerNtp last_sender_report_ntp;
	BPerSortBuffer* sortBuffer;
} BPerPlayTrack;

#if BPer_Enable_RTC_Audio
typedef struct{
	BPerPlayTrack session;
	int32_t (*on_rtp)(BPerRtcContext *context,BPerPlayTrack* session, BPerRtpPacket *ppkt);
	int32_t (*on_nack)(BPerRtcContext *context, BPerPlayTrack *track,BPerRtpPacket *pkt);
	int32_t (*check_nacks)(BPerRtcContext *context,BPerPlayTrack *audiotrack);

	int32_t (*has_ssrc)(BPerRtcContext *context, BPerPlayTrack *track,uint32_t ssrc);
	void (*update_rtt)(BPerPlayTrack *track, int32_t rtt);
	void (*update_send_report_time)(BPerPlayTrack *track, BPerNtp *ntp,	uint32_t rtp_time);
	int32_t (*send_rtcp_rr)(BPerRtcContext *context,BPerPlayTrack *track);
	int32_t (*send_rtcp_xr_rrtr)(BPerRtcContext *context,BPerPlayTrack *track) ;
}BPerPlayTrackAudio;
#endif

#if BPer_Enable_RTC_Video
typedef struct  {
	BPerPlayTrack track;
	void *h264Track;
	void *h265Track;
} BPerPlayTrackSession;

typedef struct{
	BPerPlayTrackSession session;
	int32_t (*on_rtp)(BPerRtcContext* context,BPerPlayTrackSession* track,BPerRtpPacket *pkt);
	int32_t (*on_nack)(BPerRtcContext *context, BPerPlayTrack *track,BPerRtpPacket *pkt);
	int32_t (*check_nacks)(BPerRtcContext *context,BPerPlayTrackSession *videotrack);
	void (*setRequestKeyframeState)(BPerPlayTrackSession* track,int32_t state);
	int32_t (*has_ssrc)(BPerRtcContext *context, BPerPlayTrack *track,uint32_t ssrc);
	void (*update_rtt)(BPerPlayTrack *track, int32_t rtt);
	void (*update_send_report_time)(BPerPlayTrack *track, BPerNtp *ntp,	uint32_t rtp_time);
	int32_t (*send_rtcp_rr)(BPerRtcContext *context,BPerPlayTrack *track);
	int32_t (*send_rtcp_xr_rrtr)(BPerRtcContext *context,BPerPlayTrack *track) ;
}BPerPlayTrackVideo;
#endif
typedef struct  {
	uint32_t audioSsrc;
	uint32_t videoSsrc;
	int32_t mw_msgs;
#if BPer_Enable_RTC_Audio
	BPerPublishNackBuffer *audio_queue;
#endif
#if BPer_Enable_RTC_Video
	BPerPublishNackBuffer *video_queue;
#endif

} BPerRtcPushStream;


typedef struct  {
	uint8_t request_keyframe;
	uint8_t twccFbCount;
	uint16_t twccEnabled;
	int32_t twccId;

	int64_t last_time_send_twcc;
	BPerRecvTWCC* twcc;

	BPerRtpBuffer *rtpBuffer;
	BPerRtpPacket rtp;
	BPerBuffer buf;
#if BPer_Enable_RTC_Audio
	BPerPlayTrackAudio *audioTrack;
#endif
#if BPer_Enable_RTC_Video
	BPerPlayTrackVideo *videoTrack;
#endif

} BPerRtcPlayStream;




#endif /* SRC_BPERRTC_BPERRTCSTREAM_H_ */
